package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-08-29
 * Time: 22:26
 */
public class Demo3 {
    //使用 Thread 自带的标志位
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("线程运行中...");
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    //e.printStackTrace();
                    break;
                }
            }
        });

        thread.start();
        Thread.sleep(5000);
        System.out.println("控制新线程退出");
        thread.interrupt();
    }
}
